<?php
/**
 * CakeCMS - 系统公共方法
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class Common {
	
	/**
	 * 产生一个随机字符串
	 *
	 * @param int $len        	
	 * @return string
	 */
	public function generateRand($len) {
		$c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		$rand = '';
		
		srand ( ( double ) microtime () * 1000000 );
		for($i = 0; $i < $len; $i ++) {
			$rand .= $c [rand () % strlen ( $c )];
		}
		return $rand;
	}
	
	/**
	 * 对数据进行编码转换
	 *
	 * @param $data array/string
	 *        	数组
	 * @param $input string
	 *        	需要转换的编码
	 * @param $output string
	 *        	转换后的编码
	 */
	public function array_iconv($data, $input = 'gbk', $output = 'utf-8') {
		if (! is_array ( $data )) {
			return iconv ( $input, $output, $data );
		} else {
			foreach ( $data as $key => $val ) {
				if (is_array ( $val )) {
					$data [$key] = $this->array_iconv ( $val, $input, $output );
				} else {
					$data [$key] = iconv ( $input, $output, $val );
				}
			}
			return $data;
		}
	}
	
	/**
	 * 验证邮箱
	 *
	 * @param string $str        	
	 * @return boolean
	 */
	public function checkEmail($str) {
		if (preg_match ( "/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $str ) == 1) {
			return true;
		}
		return false;
	}
}
?>